//
//  WmcPhtotoLayoutCell.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/2.
//

import UIKit

class WmcPhtotoLayoutCell: WmcBaseCollectionCell {

    private lazy var gridView: UIView = {
        let view = UIView()
        return view
    }()
    
    var layout: WmcPhotoLayout = .init() {
        didSet {
            titleLabel.text = layout.columns.info.title
            contentView.layer.borderColor = layout.isSelected ? UIColor.wmc_mainTheme.cgColor : UIColor.wmc_lineColor.cgColor
            contentView.layer.borderWidth = layout.isSelected ? 3.0 : 1.0
            
            let oldColumns = oldValue.columns
            let newColumns = layout.columns
            
            // 如果行数或者列数变化
            let numChanged = (oldColumns.info.rows != newColumns.info.rows || oldColumns.rawValue != newColumns.rawValue)
            if gridView.subviews.isEmpty || numChanged { // 如果方格为空或者行列发生变化
                let columns = newColumns.rawValue
                let rows = newColumns.info.rows
                gridView.subviews.forEach { $0.removeFromSuperview() }
                // gridView的宽高
                let gridWH = layout.itemW - padding * 2.0
                // 总共列间距
                let total_col_space = (columns - 1).yp.cgFloat * spacing
                // item的宽度
                let itemW = (gridWH - total_col_space) / columns.yp.cgFloat
                // 总共行间距
                let total_row_space = (rows - 1).yp.cgFloat * spacing
                // item的高度
                let itemH = (gridWH - total_row_space) / rows.yp.cgFloat
                // 添加方格
                let count = columns * rows
                for index in 0..<count {
                    let itemView = UIView()
                    itemView.backgroundColor = UIColor.wmc_grayH18_F0F0F0
                    gridView.addSubview(itemView)
                    // 行号
                    let row = index / columns
                    // 列号
                    let column = index % columns
                    // 设置frame
                    let x = (itemW + spacing) * column.yp.cgFloat
                    let y = (itemH + spacing) * row.yp.cgFloat
                    itemView.frame = CGRect(x: x, y: y, width: itemW, height: itemH)
                }
            }
        }
    }
    private let padding: CGFloat = 4.0.wmc_autoHor
    private let spacing: CGFloat = 3.0.wmc_autoHor
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        // contentView
        contentView.layer.cornerRadius = 2.0
        contentView.clipsToBounds = true
        // 标题
        titleLabel.textAlignment = .center
        titleLabel.font = 11.wmc_scaleFont(weight: .medium)
        titleLabel.textColor = UIColor.wmc_blackH1_000000
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(padding)
            make.left.equalTo(padding)
            make.right.equalTo(-padding)
        }
        // gridView
        contentView.addSubview(gridView)
        gridView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(2.0.wmc_autoHor)
            make.left.right.equalTo(titleLabel)
            make.height.equalTo(gridView.snp.width)
        }
    }

}
